package com.lex.activitytext;


import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class TestActivity extends Activity {
    private Button btn;
    private TextView tv;
    private EditText edit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);

        btn = (Button) findViewById(R.id.btn);
        tv = (TextView) findViewById(R.id.tv);
        edit = (EditText) findViewById(R.id.edit);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new NetworkGteThread().start();
//                new NetworkPostThread().start();
                btn.setText("请求中...");
                btn.setEnabled(false);
            }
        });
    }

    class NetworkGteThread extends Thread {
        @Override
        public void run() {
            HttpClient httpClient = new DefaultHttpClient();

            List<BasicNameValuePair> params = new ArrayList<>();
            BasicNameValuePair wordParam = new BasicNameValuePair("word","Android");
            BasicNameValuePair pageParam = new BasicNameValuePair("page","1");
            params.add(wordParam);
            params.add(pageParam);
            String paramStr = URLEncodedUtils.format(params,"UTF-8");//work = Android & page = 1

            //HttpGet httpGet = new HttpGet(edit.getText().toString() + "?" + paramStr);
            //下面两句等价
            //HttpGet httpGet = new HttpGet("http://www/baidu.com/baidu" + "?" + paramStr);
            HttpGet httpGet = new HttpGet("http://www.kuaidi100.com/query?type=zhongtong&postid=405123024058");
            try {
                HttpResponse httpResponse = httpClient.execute(httpGet);
                if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                    HttpEntity httpEntity = httpResponse.getEntity();
                    final String entityStr = EntityUtils.toString(httpEntity,"UTF-8");
                    Log.d("lex", entityStr);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            btn.setEnabled(true);
                            btn.setText("请求");
                            tv.setText(entityStr);
                            tv.append("\n解析结果为:\n");
                            CaseInfoEntity caseInfoEntity = CaseInfoEntity.personFromJsonStr(entityStr);
                            tv.append(caseInfoEntity.toString());
                        }
                    });
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private String parseJsonStr(String jsonStr){
        try{
            JSONObject jsonObject = new JSONObject(jsonStr);
            int status = jsonObject.getInt("status");
            String msg = jsonObject.getString("message");
            return "状态码为："+status+"\n消息为："+msg;
        }catch (JSONException e){
            e.printStackTrace();
        }
        return "解析错误";
    }

}